package
{
	import com.designoutput.elements.ImageElement;
	import com.designoutput.manager.CommandManager;
	import com.designoutput.manager.CursorManager;
	import com.designoutput.manager.FocusManager;
	import com.designoutput.manager.FontManager;
	import com.designoutput.manager.PopUpManager;
	import com.designoutput.manager.ResourceManager;
	import com.designoutput.manager.ValidationManager;
	import com.designoutput.service.CardService;
	import com.designoutput.service.ServiceBase;
	import com.designoutput.system.FpsBox;
	import com.designoutput.ui.Card;
	import com.designoutput.ui.CardWindow;
	import com.designoutput.ui.FacesToolBar;
	import com.designoutput.ui.GalleryCanvas;
	import com.designoutput.ui.Hover;
	import com.designoutput.ui.LeftToolBar;
	import com.designoutput.ui.TabMenuBar;
	import com.designoutput.ui.TopToolBar;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.CardTypeCollectionVo;
	
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
		
	[SWF(backgroundColor=0x606060)]
	[Frame(factoryClass="com.preloader.CustomPreloader")]
	public class DesignoutputCard extends ControlBase
	{			
		public var _fps:FpsBox;
		
		public var hover:Hover;
		
		private var _tabMenubar:TabMenuBar;
		
		private var _content:Sprite;
		private var _layoutHolder:Sprite;// The holder contains the layout elements, such as the Canvas,LeftToolBar,TopToolBar etc.
		private var _galleryCanvas:GalleryCanvas;
		
		//Layout things
		private var _cardOptionsToolBar:FacesToolBar;
		public var leftToolbar:LeftToolBar; 
		public var topToolbar:TopToolBar; 
		public var canvas:CardWindow;
		public var card:Card;
		
		public var tool:String;
		
		public var loader:URLLoader;
		public var cardTypeCollection:CardTypeCollectionVo;
				
		public var command:CommandManager;
		protected var _stage:*;		
		protected var _loaderInfo:LoaderInfo;
		
		public function DesignoutputCard(stageValue:*=null, loaderInfoValue:LoaderInfo=null) {
			this._stage = stageValue;			
			this._loaderInfo = loaderInfoValue;
			super();
		}
		
		override protected function initialize():void {			
			setApplicationUrl();
			
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.stageFocusRect = false;
			_stage.addEventListener(Event.RESIZE, stage_resizeHandler, false, 0, true);			
			
			initializeInfrastructure();
			
			command = new CommandManager(this);
			
			Globals.fontManager.onFontListReady(loadCardTypes);
		}
		
		private function initializeInfrastructure():void {
			ResourceManager.init();
			
			Globals.designoutputCard = this;
			Globals.fontManager = new FontManager();
			Globals.validationManager = new ValidationManager(_stage);
			Globals.focusManager = new FocusManager(_stage);
			
			//Content Holder
			_content = new Sprite();
			addChild(_content);
			
			//Popup manager
			Globals.popupManager = new PopUpManager();
			addChild(Globals.popupManager);
			
			//Cursor Manager
			Globals.cursorManager = new CursorManager();
			addChild(Globals.cursorManager);
			
			//Hover
			hover = new Hover(this);
			addChild(hover);
			
			_fps = new FpsBox();
			addChild(_fps);			
		}
		
		private function setApplicationUrl():void {			
			var swfUrl:String = _loaderInfo.url;			
			
			if(swfUrl.indexOf("file://") == -1)
				Globals.applicationUrl = swfUrl.substring(0, swfUrl.lastIndexOf("/")+1); 
			else
				Globals.applicationUrl = "http://localhost/DesignOutput/";			
			
			ServiceBase.BASE_SERVICE_URL = Globals.applicationUrl + "services/";			
		}
		
		private function loadCardTypes():void{
			var request:URLRequest = new URLRequest("config/xml/cardTypes.xml");
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, cardTypesLoadedHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
			loader.load(request);
		}
		
		private function cardTypesLoadedHandler(evt:Event):void{
			var cardXml:XML = XML(evt.target.data);
			cardTypeCollection = CardTypeCollectionVo.parseFromXML(cardXml);
			
			initializeWorkArea();
						
			if(_loaderInfo.parameters.cardId && _loaderInfo.parameters.cardId != "new")
				openCard(int(_loaderInfo.parameters.cardId));
			
			//openCard(57);
		}
		
		public function openCard(cardId:int):void {
			CardService.instance.openCard(cardId, cardOpenComplete, cardOpenError);
		}
		
		protected function cardOpenComplete(result:Object):void {
			var cardXML:XML = new XML(result);
			
			var shared:int = int(cardXML.@shared);
			
			if(shared == 0)
				card.setCardData(int(cardXML.@id), String(cardXML.@name));
			else
				card.setCardData(0, "");
			
			card.fromXML(cardXML.CardPage[0]);
		}
		
		protected function cardOpenError(result:Object):void {
		}
		
		private function loadErrorHandler(evt:IOErrorEvent):void{
			trace(evt.toString());
		}
				
		private function initializeWorkArea():void{
			_layoutHolder = new Sprite;
			_content.addChild(_layoutHolder);
			
			//Card Window		
			canvas = new CardWindow();
			card = canvas.card;
			
			//Tab menu bar
			_tabMenubar = new TabMenuBar(this);
			_content.addChild(_tabMenubar);
			
			//Left Toolbar			
			leftToolbar = new LeftToolBar();
			leftToolbar.x = 10;
			leftToolbar.y = 65;
			
			//Top Toolbar
			topToolbar = new TopToolBar();
			topToolbar.y = 22;

			//Gallery Canvas
			_galleryCanvas = new GalleryCanvas();
			_galleryCanvas.visible = false;// default show layout only.
			_content.addChild(_galleryCanvas);

			_layoutHolder.addChild(leftToolbar);
			_layoutHolder.addChild(topToolbar);
			_layoutHolder.addChild(canvas);
			
			invalidateDisplay();
			
			_cardOptionsToolBar = new FacesToolBar();
			_layoutHolder.addChild(_cardOptionsToolBar);
		}
		
		protected function stage_resizeHandler(e:Event):void {
			width = _stage.stageWidth;
			height = _stage.stageHeight;
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			if (_tabMenubar)
				_tabMenubar.Resize();
			
			if(topToolbar)
				topToolbar.Resize();
			
			if(Globals.popupManager){
				Globals.popupManager.width = _stage.stageWidth;
				Globals.popupManager.height = _stage.stageHeight;
			}
			
			if(_galleryCanvas){
				_galleryCanvas.x = 0;
				_galleryCanvas.y = 25;
				_galleryCanvas.width = _stage.stageWidth;
				_galleryCanvas.height = _stage.stageHeight - 25;
			}
			
			if(_cardOptionsToolBar){
				_cardOptionsToolBar.x = _stage.stageWidth - _cardOptionsToolBar.width - 10;
				_cardOptionsToolBar.y = 65;
			}
			
			if(canvas && canvas.width > _cardOptionsToolBar.x - 5 - canvas.x)
				canvas.width = _cardOptionsToolBar.x - 5 - canvas.x;
			
			_fps.x = _stage.stageWidth - 100;
			_fps.y = _stage.stageHeight - 18;
		}
		
		public function setTool(type:String):void{
			tool = type;
			Globals.cursorManager.setCursor(tool);
			canvas.setToolType(tool);
		}
		
		public function getTool():String
		{
			return tool;
		}

		
		public function resetTool():void
		{
			setTool(tool);
		}
		
		public function showLayout():void {
			_layoutHolder.visible = true;
			_galleryCanvas.visible = false;
		}
		
		public function showGallery():void {
			_layoutHolder.visible = false;
			_galleryCanvas.visible = true;	
		}
		
		public function chooseImage(element:ImageElement, bSwitch:Boolean):void{
			if(bSwitch)
				showGallery();
			
			_galleryCanvas.chooseImage(element);
		}
	}
}